//utility function to compare URLs
var urlList = new Array();
urlList[0] = "http://twitter.com/status/update";
urlList[1] = "http://www.facebook.com/ajax/updatestatus.php";
urlList[2] = "http://www.blogger.com/post-create.do";
urlList[3] = "http://www.facebook.com/ajax/profile/editinfo.php?__a=1"; //xhr
urlList[4] = "twitter.com/settings/profile"; //html
urlList[5] = "blogger.com/edit-profile.do"; //html
urlList[6] = "user/editadvanced.php"; //moodle profile html
urlList[7] = "http://www.facebook.com/ajax/ufi/modify.php";//fb comments


function mapXhrPost(url ,data)
{	 
var msg = null;
       for(var i=0; i<urlList.length;i++){
       	if(url.indexOf(urlList[i])!=-1){ //return match only
       		switch(i)
       		{
       		case 0:
       			msg = twPost(url,data);
       		break;
       		case 1:
           		msg =fbPost(url,data);
           		break;
       		case 3:
           		msg ="<PROFILE><Element elementName=\"Site\">"+url+"</Element>"+data+"</PROFILE>";
           		break;
       		default:
			msg ="<POST><Element elementName=\"Site\">"+url+"</Element>"+data+"</POST>";
       		}
       		
       	}
       }
//alert("XHR MSG:"+msg);
       return msg;
}

function mapHtmlPost(url ,data)
{
//alert(url);
	 var msg = null;
       for(var i=0; i<urlList.length;i++){
       	if(url.indexOf(urlList[i])!=-1){
       		switch(i)
       		{
           	case 2:
           		msg =bgPost(url,data);
           		break;
           	case 4:
           		msg ="<PROFILE>"+htmlForm(url,data)+"</PROFILE>";
           		break;
           	case 5:
           		msg ="<PROFILE>"+htmlForm(url,data)+"</PROFILE>";
           		break;
           	case 6:
           		msg ="<PROFILE>"+htmlForm(url,data)+"</PROFILE>";
           		break;
     		default:
			msg = "<POST>"+htmlForm(url,data)+"</POST>";
       		}
       		
       	}
       }
//alert("HTML MSG:"+msg);
       return msg;
}

function htmlForm(url,data)
{
var form = data; var value; var id; var str ="";
for (var i=0;i<form.length;i++)
{
if((form.elements[i].value)!= undefined && (form.elements[i].name)!= undefined)
{
value =form.elements[i].value.toString();
id =form.elements[i].name.toString();
str += "<Element elementName=\""+id+"\">"+value+ "</Element>"+"\n";
}
}
var msg = "<Element elementName=\"Site\">"+url+"</Element>"+str;
return msg;
}

function bgPost(url, data)
{	
var form = data;
var str = ""; 
var value; var id; 
var blog="";
for (var i=0;i<form.length;i++)
{
if((form.elements[i].value)!= undefined && (form.elements[i].id)!= undefined)
{
value =form.elements[i].value.toString();
id =form.elements[i].id.toString();
if(id.indexOf("f-title")!=-1)
{
str += "<Element elementName=\"UpdateData\">"+value+ "</Element>"+"\n";
//alert(str);//BUG FIX LATER!!
}
else if(id == "blogURL"){//TODO error checking
blog = value;
////get latest post url
$.get(blog, function(response) {
try
{
var test = response.toString();
var array = test.split('post-title');
//alert(array[1]);
array = array[1].split("href='");
//alert(array[1]);
array = array[1].split("'>");
str += "<Element elementName=\"BlogPostUrl\">"+array[0]+ "</Element>"+"\n";
//alert(str);//BUG FIX LATER!!
}
catch(e1)
{
alert(e1.toString());
}
	});
}
}
}
//alert(str);//BUG FIX LATER!!
var msg = "<STS>\n<Element elementName=\"Site\">"+url+"</Element>"+str+"</STS>";
return msg;
}

function fbPost(url, data)
{
	try
	{
	var test =null ,str ="", targetId = null; sts =null , profileId=null;
	test = data.split("target_id\">");
	if(test[1]!=undefined)
	{
	test = test[1].split("<");
	targetId = test[0];
	}
	
	test = data.split("status\">"); //more
	if(test[1]!=undefined)
	{
	test = test[1].split("<");
	sts = test[0] ;
	}
	
	test = data.split("profile_id\">");
	if(test[1]!=undefined)
	{
	test = test[1].split("<");
	profileId = test[0] ;
	}
	
	if(sts!=null)
	{
	str += "<Element elementName=\"UpdateData\">"+sts+ "</Element>"+"\n";
	}
	if(targetId !=0 && targetId!=null && targetId!=profileId)
	{
	str += "<Element elementName=\"TargetProfile\">"+targetId+ "</Element>"+"\n";
	}
	var msg = "<STS>\n<Element elementName=\"Site\">"+url+"</Element>"+str+"</STS>";
	return msg;
	}
	catch (e)
	{
		return "ERROR";
	}
}

function twPost(url, data)
{
	var post = data;
	var test ;var str =""; var sts =null ; var replyTo=null;
	test = post.split("in_reply_to_status_id\">");
	if(test[1]!=undefined)
	{
	test = test[1].split("<");
	replyTo = test[0];
	}
	test = post.split("=\"status\">");//more
	if(test[1]!=undefined)
	{
	//alert(test[1]);
	test = test[1].split("<");
	sts = test[0] ;
	}
	if(sts!=null)
	{
	str += "<Element elementName=\"UpdateData\">"+sts+ "</Element>"+"\n";
	}
	if(replyTo!=null)
	{
	str += "<Element elementName=\"ReplyToStatus\">"+replyTo+ "</Element>"+"\n";
	}
	var msg = "<STS>\n<Element elementName=\"Site\">"+url+"</Element>"+str+"</STS>";
	return msg;
}



/*
FB
<STS>
<elementID="Site"></>
<elementID="UpdateData"></>
<elementID="TargetProfile"></>
</STS>
TW
<STS>
<elementID="Site"></>
<elementID="UpdateData"></>
<elementID="ReplyToStatus"></>
</STS>
BG
<STS>
<elementID="Site"></>
<elementID="UpdateData"></>
<elementID="BlogPostUrl"></>
</STS>


 */





